import { userLogin } from '@/api/login'
import router from '@/router'
import { Message } from 'element-ui'
export default {
  namespaced: true,
  state: {
    token: localStorage.getItem('token') || ''
  },
  mutations: {
    userLogin (state, res) {
      // console.log(res)
      state.token = res.data.token
      localStorage.setItem('token', res.data.token)
    },
    delToken (state) {
      localStorage.removeItem('token')
      state.token = ''
    }
  },
  actions: {
    // 用户登录
    async getUserLogin ({ commit }, formDate) {
      const res = await userLogin(formDate)
      if (res.meta.status === 200) {
        commit('userLogin', res)
        router.push('/')
      } else {
        Message.error(res.meta.msg)
      }
    }
  }
}
